const app = getApp();
const defaultAvatarUrl = '../../icon/avatar.png'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: defaultAvatarUrl,
        theme: wx.getSystemInfoSync().theme,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that  = this;
        wx.onThemeChange((result) => {
            this.setData({
              theme: result.theme
            })
          })
          var deviceInfo = wx.getDeviceInfo();
          console.log(deviceInfo)
          let d = app.globalData;
          var open=wx.getStorageSync('open');  
          this.setData({
              //id:id,
              open:open,
              width:d.windowWidth,
              height:d.windowHeight,
              statusBarHeight:d.statusBarHeight,
              navHeight:d.navHeight,
              marginTop:d.navTitleTop,
              bottom:d.bottom,
              domain:d.domain,
              imgUrl:d.img,
              platform:deviceInfo.platform
          })
    },

    onChooseAvatar(e) {
        const { avatarUrl } = e.detail 
        this.setData({
          avatarUrl,
        })
      },
    formSubmit(e){
       // console.log(e);
       //var open = wx.getStorageSync('open');
       var open = wx.getStorageSync('open');
       console.log(open);
       var avatar = this.data.avatarUrl;
       var nickname = e.detail.value.nickname;
       if(nickname == ''){
            wx.showModal({
              title: '重要提示',
              content: '请选择或者输入昵称',
              complete: (res) => {
                if (res.cancel) {
                    return false;
                }
            
                if (res.confirm) {
                    return false;
                }
              }
            })
       }
       if( avatar == '../../icon/avatar.png' ){
        wx.showModal({
            title: '重要提示',
            content: '请选择或者上传头像ß',
            complete: (res) => {
              if (res.cancel) {
                  return false;
              }
          
              if (res.confirm) {
                  return false;
              }
            }
          })
       }
       wx.uploadFile({
         filePath: avatar,
         url: app.globalData.domain + 'wechat/mine/setNickAvatar', //仅为示例，非真实的接口地址
       // filePath: tempFilePaths[0],
        name: 'file',
        formData: {
            'nickname': nickname,
            'imageType':'avatar',
            'open_id':open.id
         },
         success (res){
             console.log(res)
            const data = res.data
            console.log(data);
            var d = JSON.parse(data);
            console.log(d.open_id);
            var open = d.open;
            console.log(open);
            if(res.statusCode==200){
                wx.setStorageSync('open', open);
                wx.showModal({
                  title: '重要提醒',
                  content: '更新成功',
                  complete: (res) => {
                    wx.navigateBack();
                  }
                })
            }
           
          }
       })
    },
    goBack:function(){
        wx.navigateBack();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})